/***********************************
 *           3/24/2014             *
 *       by Meow dev (arm genesis) *
 *                                 *
 ***********************************/
package com.meow.dev.engine;

import java.util.logging.Logger;

import com.meow.dev.constante.EnrollmentConstance;
import com.meow.dev.constante.ErrorCode;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.LongByReference;

public class RealTimeDataEngine implements Runnable {
	private final static Logger LOGGER = Logger.getLogger(RealTimeDataEngine.class.getName());

	private NativeLong connection;
	private FKAttendDLLMapping attendDLLMapping;
	private RealTimeDataListener realTimeDataListener = null;
	private PB7Engine engine;
	
	private boolean running = false;
	private boolean pause = false;
	

	public RealTimeDataEngine(FKAttendDLLMapping attendDLLMapping,NativeLong connection) {
		this.connection = connection;
		this.attendDLLMapping = attendDLLMapping;
	}
	public RealTimeDataEngine(PB7Engine engine) {
		this(engine.getAttendDLLMapping(),engine.getConnectionResult());
		this.engine = engine;
	}

	@Override
	public void run() {
		long start = System.currentTimeMillis();
		
		LongByReference apnEnrollNumber = new LongByReference();
		LongByReference pnVerifyMode = new LongByReference();
		LongByReference apnInOutMode = new LongByReference();
		LongByReference apnYear = new LongByReference();
		LongByReference apnMonth = new LongByReference();
		LongByReference apnDay = new LongByReference();
		LongByReference apnHour = new LongByReference();
		LongByReference apnMinute = new LongByReference();
		LongByReference apnSec = new LongByReference();
		
		attendDLLMapping.FK_LoadGeneralLogData(connection, EnrollmentConstance.ReadMark_READ_NEW);
		NativeLong res = ErrorCode.RUNERR_NON_CARRYOUT;
		
		running = true;
			do {
			res= attendDLLMapping.FK_GetGeneralLogData_1(connection,
					apnEnrollNumber, pnVerifyMode, apnInOutMode, apnYear, apnMonth,
					apnDay, apnHour, apnMinute, apnSec);
			if (res.equals(ErrorCode.RUN_SUCCESS)) {
				UserLog userLog = new UserLog();
				userLog.setUserId(apnEnrollNumber.getValue());
				userLog.setVerifyMode(pnVerifyMode.getValue());
				userLog.setInOutMode(apnInOutMode.getValue());
				userLog.setYear(apnYear.getValue());
				userLog.setMonth(apnMonth.getValue());
				userLog.setDay(apnDay.getValue());
				userLog.setHour(apnHour.getValue());
				userLog.setMin(apnMinute.getValue());
				userLog.setSec(apnSec.getValue());

				if(realTimeDataListener != null){
					realTimeDataListener.onNewLog(this.engine , userLog);
				}
				System.out.println(userLog);
			}else{
				LOGGER.info("# Error ## " + res);
//				running = false;
				if(res.equals(ErrorCode.RUNERR_DATAARRAY_END)){
					res = attendDLLMapping.FK_LoadGeneralLogData(connection, EnrollmentConstance.ReadMark_READ_NEW);
					LOGGER.info("## 2 ##" + res);
				}
				if(res.equals(ErrorCode.RUNERR_NOSUPPORT)){
					LOGGER.info("@@ Server Fail Down");
				}
			}
			
			try {
				//sleep
				Thread.sleep(1000);
			
			if(pause){
				if(realTimeDataListener != null){
					LOGGER.info("### PAUSE #####");
					realTimeDataListener.onPause();
				}
				// pause handle
				while(pause){
					Thread.sleep(500);
				}
				
			}
			
			} catch (InterruptedException e) {
				e.printStackTrace();
				LOGGER.info("$$$$ " + e.getMessage());
				running = false;
			}
		}while(running);

			
		long end = System.currentTimeMillis();
		LOGGER.info("######## time running : " + (end - start) + "  mili sec");
		
	}
	
	public void pause(){
		pause = true;
	}
	public void resume(){
		pause = false;
	}
	public void stop(){
		running = false;
	}
	
	
	public boolean isRunning() {
		return running;
	}

	public interface RealTimeDataListener{
		abstract public void onNewLog(PB7Engine engine , UserLog userLog);
		abstract public void onPause( );
	}
	
	public  void setRealTimeDataListener(RealTimeDataListener realTimeDataListener) {
		this.realTimeDataListener = realTimeDataListener;
	}

	public static void main(String[] args) {
		PB7Engine pb7Engine = new PB7Engine();
		pb7Engine.connect();
		RealTimeDataEngine realTimeData = new RealTimeDataEngine( pb7Engine.getAttendDLLMapping(), pb7Engine.getConnectionResult());
		Thread t = new Thread(realTimeData);
		t.start();
	}
	
	
}
